/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import http from '@ohos.net.http';
import prompt from '@system.prompt';
import router from '@system.router';

export default {
    data: {
        userId:'',
        name:'',
        relation:0,
        token:''
    },
    onInit() {
    },



    fetch(){
        let httpRequest = http.createHttp();
        // 用于订阅http响应头，此接口会比request请求先返回。可以根据业务需要订阅此消息
        httpRequest.on('headerReceive', (err, data) => {

        });
        httpRequest.request(
        // 填写http请求的url地址，可以带参数也可以不带参数。URL地址需要开发者自定义。GET请求的参数可以在extraData中指定
            "http://8.142.187.137:8888/user/addFriend",
            {
                method: 'POST', // 可选，默认为“GET”
                // 开发者根据自身业务需要添加header字段
                header: {
                    'Content-Type': 'application/json',
                    'token': this.token
                },
                // 当使用POST请求时此字段用于传递内容
                extraData: {
                    'user2Id': this.userId, 'name': this.name, 'relationType': this.relation
                },
                readTimeout: 60000, // 可选，默认为60000ms
                connectTimeout: 60000 // 可选，默认为60000ms
            },(err, data) => {
            if (!err) {
                // data.result为http响应内容，可根据业务需要进行解析
                var result = JSON.parse(data.result);
                console.info('Resultaddfriend--:' + data.result);
                console.info('code:' + data.responseCode);
                // data.header为http响应头，可根据业务需要进行解析
                console.info('header:' + data.header);
                if(result["code"] == '200'){
                    prompt.showToast({
                        message: result.data
                    })

//                    router.push({
//                        uri: '',
//                        params: {
//                            userId: ''
//                        },
//                    })


                }else if(result["code"] == '400'){
                    prompt.showToast({
                        message: result.msg
                    })
                }

            } else {
                console.info('error:' + err.data);
            }
        }
        );
    },





    getName(e) {
        this.name = e.value;
        console.info("name=" + this.name)
    },

    getIdentity(e) {

        this.identity = e.newValue;
        if(this.identity == '家长') {
            this.relation = 1;
        }else if(this.identity == '教师'){
            this.relation = 2;
        }
        console.info("identity=" + this.identity);
    },
    backClick(){
        router.back();
    },

    onAddFriend() {
        if (this.name.length == 0) {
            prompt.showToast({
                message: "备注名为空"
            })
            return;
        }
        this.fetch();
    }
}
